﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBHelper;
using BusinessLayer.RegInfo.Entities;
using System.Data.SqlClient;
using System.Data;

namespace BusinessLayer.RegInfo
{
    public class AccountBL
    {
        private readonly DbHelper _db;

        public AccountBL()
        {
            _db = new DbHelper(new DbConfig().DbRegInfoConnectionString);         
        }

        public void Insert(Account account, int subscriptionId, int typePay, string couponCode, string merchTxnRef, out decimal amount)
        {
            var oCommand = new SqlCommand("sp_Account_Insert") { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@FirstName", account.FirstName));
            oCommand.Parameters.Add(new SqlParameter("@LastName", account.LastName));
            oCommand.Parameters.Add(new SqlParameter("@Email", account.Email));
            oCommand.Parameters.Add(new SqlParameter("@PassWord", account.PassWord));
            oCommand.Parameters.Add(new SqlParameter("@Country", account.Country));
            oCommand.Parameters.Add(new SqlParameter("@Find", account.Find));
            oCommand.Parameters.Add(new SqlParameter("@SubscriptionId", subscriptionId));
            oCommand.Parameters.Add(new SqlParameter("@TypePay", typePay));
            oCommand.Parameters.Add(new SqlParameter("@CouponCode", couponCode));
            oCommand.Parameters.Add(new SqlParameter("@MerchTxnRef", merchTxnRef));
            var output = new SqlParameter("@Amount", SqlDbType.Decimal) { Direction = ParameterDirection.Output };
            oCommand.Parameters.Add(output);
            _db.ExecuteNonQuery(oCommand);
            amount = (decimal)output.Value;
        }

        public void Update(Account account)
        {
            var oCommand = new SqlCommand("sp_Account_Update") { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@Id", account.Id));
            oCommand.Parameters.Add(new SqlParameter("@FirstName", account.FirstName));
            oCommand.Parameters.Add(new SqlParameter("@LastName", account.LastName));
            oCommand.Parameters.Add(new SqlParameter("@Email", account.Email));
            oCommand.Parameters.Add(new SqlParameter("@PassWord", account.PassWord));
            oCommand.Parameters.Add(new SqlParameter("@Country", account.Country));
            oCommand.Parameters.Add(new SqlParameter("@Find", account.Find));
            _db.ExecuteNonQuery(oCommand);
        }

        public Account GetByEmail(string email)
        {
            var oCommand = new SqlCommand("sp_Account_Select_By_Email") { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@Email", email));

            var list = _db.GetList<Account>(oCommand);
            return list.FirstOrDefault();
        }

        public Account GetByMerchTxnRef(string merchTxnRef)
        {
            var oCommand = new SqlCommand("sp_Account_Select_By_MerchTxnRef") { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@MerchTxnRef", merchTxnRef));

            var list = _db.GetList<Account>(oCommand);
            return list.FirstOrDefault();
        }

        public void ActiveAccount(string tokenKey, string merchTxnRef,out int result)
        {
            var oCommand = new SqlCommand("sp_ActiveAccount") { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@TokenKey", tokenKey));
            oCommand.Parameters.Add(new SqlParameter("@MerchTxnRef", merchTxnRef));
            var output = new SqlParameter("@Result", SqlDbType.Int) { Direction = ParameterDirection.Output };
            oCommand.Parameters.Add(output);
            _db.ExecuteNonQuery(oCommand);
            result = (int)output.Value;
        }
    }
}
